/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language enrstuing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.enrstu.rest;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.enrstu.domain.School;
import me.zhengjie.modules.enrstu.service.SchoolDistrictService;
import me.zhengjie.modules.enrstu.service.SchoolService;
import me.zhengjie.modules.enrstu.service.dto.*;
import me.zhengjie.modules.enrstu.util.excel.EasyExcelUtil;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.resp.Result;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
* @website https://eladmin.vip
* @author yyx
* @date 2024-10-14
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "招生系统：学校管理")
@RequestMapping("/api/enrstu/school")
public class SchoolController {

    private final SchoolService schoolService;
    private final SchoolDistrictService schoolDistrictService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('school:list')")
    public void exportSchool(HttpServletResponse response, SchoolQueryCriteria criteria) throws IOException {
        schoolService.download(schoolService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询学校管理")
    @ApiOperation("查询学校管理")
    @PreAuthorize("@el.check('school:list')")
    public ResponseEntity<PageResult<SchoolDto>> querySchool(SchoolQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(schoolService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/byId/{schoolId}")
    @Log("根据id查询学校管理信息")
    @ApiOperation("根据id查询学校管理信息")
    @PreAuthorize("@el.check('school:list')")
    public ResponseEntity<SchoolDto> queryByIdSchool(@PathVariable String schoolId){
        return new ResponseEntity<>(schoolService.findById(schoolId),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增学校管理")
    @ApiOperation("新增学校管理")
    @PreAuthorize("@el.check('school:add')")
    public ResponseEntity<Object> createSchool(@Validated @RequestBody School resources){
        schoolService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改学校管理")
    @ApiOperation("修改学校管理")
    @PreAuthorize("@el.check('school:add')")
    public ResponseEntity<Object> updateSchool(@Validated @RequestBody SchoolCreateOrUpdate resources){
        schoolService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping("/{id}")
    @Log("删除学校管理")
    @ApiOperation("删除学校管理")
    @PreAuthorize("@el.check('school:add')")
    public ResponseEntity<Object> deleteSchool(@PathVariable String id) {
        schoolService.deleteAll(id);
        return new ResponseEntity<>(Result.ok(),HttpStatus.OK);
    }
    @GetMapping("/query/districtStat")
    @Log("获取学区信息")
    @ApiOperation("获取学区信息")
    @PreAuthorize("@el.check('school:list')")
    public ResponseEntity<List<SchoolDistrictCommonServiceDto>> queryDistrictStat(){
        return new ResponseEntity<>(schoolDistrictService.querySchoolDistrict(),HttpStatus.OK);
    }



    @GetMapping("/query/educationList")
    @Log("获取教育局列表")
    @ApiOperation("获取教育局列表")
    @PreAuthorize("@el.check('school:list')")
    public ResponseEntity<List<Dept>> queryEducationList(){
        return new ResponseEntity<>(schoolService.queryEducationAll(),HttpStatus.OK);
    }
    @PostMapping("/import")
    @Log("导入学校")
    @ApiOperation("导入学校")
    @PreAuthorize("@el.check('school:add')")
    public ResponseEntity<Object> importSchool(@RequestParam("file") MultipartFile file, String sheetName) throws IOException {
        if (ObjectUtil.isNull(sheetName)){
            throw new BadRequestException("请指定上传文件具体的sheet页");
        }
        List<School> schools = EasyExcelUtil.batchImport(School.class, file, sheetName);
        log.info("上传文件成功，开始导入数据:{}",schools);
        schools.forEach(schoolService::create);
        log.info("上传文件成功，开始导入数据:{}",schools);
        return new ResponseEntity<>(Result.ok(),HttpStatus.OK);
    }
    @GetMapping("/download/template")
    @Log("导出导入学校模版")
    @ApiOperation("导出导入学校模版")
    @PreAuthorize("@el.check('school:add')")
    public ResponseEntity<Object> downloadTemplate(HttpServletResponse response) throws IOException {
        schoolService.download(response);
        return new ResponseEntity<>(Result.ok(),HttpStatus.OK);
    }

}